package com.projectzqjz.parttimework.net;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.base.Constant;
import com.projectzqjz.parttimework.entity.PartBaseEntity;
import com.projectzqjz.parttimework.ui.activity.PartLoginActivity;
import com.projectzqjz.parttimework.utils.NewsToastUtils;
import com.projectzqjz.parttimework.utils.SaveParameterUtils;
import com.projectzqjz.parttimework.utils.UserUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class MyCallback<T> implements Callback<T> {
    private Activity activity;
    private Response responseNet;

    protected MyCallback(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onResponse(Call call, Response response) {
        responseNet = response;
        SaveParameterUtils.saveToken(response.raw().header(Constant.CommonInfo.TOKEN));
        switch (response.code()) {
            case 403:
                UserUtils.clearUserInfo(); //等于401的时候得清除所有的用户信息
                activity.startActivity(new Intent(activity, PartLoginActivity.class));
                break;
            case 500:
                NewsToastUtils.showToast(App.getContext(), "服务器错误！");
                break;
            case 200:
                verifyToken(response);
                break;
        }
    }

    @Override
    public void onFailure(Call call, Throwable t) {
        onFailure(t);
    }

    protected abstract void onSuccess(Response<T> response);


    protected abstract void onFailure(Throwable t);

    private void verifyToken(Response<PartBaseEntity> response) {
        String code = response.body().getCode();
        switch (code) {
            case "4003":
                String dataForb = response.body().getData().toString();
                String msgForb = response.body().getMsg();
                UserUtils.clearUserInfo(); //等于401的时候得清除所有的用户信息
                activity.finish();
                activity.startActivity(new Intent(activity, PartLoginActivity.class)
                        .putExtra("data", dataForb)
                        .putExtra("msg", msgForb));
                break;
            case "4004":
                String dataDel = response.body().getData().toString();
                String msgdel = response.body().getMsg();
                UserUtils.clearUserInfo(); //等于401的时候得清除所有的用户信息
                activity.finish();
                activity.startActivity(new Intent(activity, PartLoginActivity.class).putExtra("data", dataDel)
                        .putExtra("msg", msgdel));
                break;
            case "200":
                onSuccess(responseNet);
                break;
            case "401":
                activity.finish();
                activity.startActivity(new Intent(activity, PartLoginActivity.class));
                NewsToastUtils.showToast(App.getContext(), response.body().getMsg());
                break;
            case "403":
                Toast.makeText(activity, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                UserUtils.clearUserInfo(); //等于401的时候得清除所有的用户信息
                activity.finish();
                activity.startActivity(new Intent(activity, PartLoginActivity.class));
                break;
            case "500":
                NewsToastUtils.showToast(App.getContext(), "系统内部错误！");
                break;

        }
    }
}
